package com.wangdao.wechatmall.service.admin;

import com.wangdao.wechatmall.bean.bo.admin.StatUserBO;
import com.wangdao.wechatmall.bean.vo.admin.StatGoodsMapperVO;
import com.wangdao.wechatmall.bean.vo.admin.StatGoodsVO;
import com.wangdao.wechatmall.bean.vo.admin.StatOrderMapperVO;
import com.wangdao.wechatmall.bean.vo.admin.StatOrderVO;
import com.wangdao.wechatmall.bean.vo.admin.dto.UserStatDTO;
import com.wangdao.wechatmall.mapper.GoodsMapper;
import com.wangdao.wechatmall.mapper.OrderMapper;
import com.wangdao.wechatmall.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * StatServiceImpl
 * created by feng on 7/9/21
 */

@Service
public class StatServiceImpl implements StatService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public StatUserBO user() {
        List<UserStatDTO> userStatDTOList = userMapper.countUserGroupByCreateDate();

        // return statUserBO
        StatUserBO statUserBO = new StatUserBO();

        // columns
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("users");

        statUserBO.setColumns(columns);

        // Rows
        statUserBO.setRows(userStatDTOList);

        // return
        return statUserBO;
    }

    @Override
    public StatOrderVO order() {
        //get StatOrder
        List<StatOrderMapperVO> statOrder = orderMapper.getOrderOrderByCreateDate();

        // return StatOrder
        StatOrderVO statOrderVO = new StatOrderVO();

        //set columns
        List<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("customers");
        columns.add("amount");
        columns.add("pcr");
        statOrderVO.setColumns(columns);

        //set Rows
        List<StatOrderVO.RowsDTO> rowsDTOS = new ArrayList<>();

        for (StatOrderMapperVO order : statOrder) {
            StatOrderVO.RowsDTO rowsDTO = new StatOrderVO.RowsDTO();
            rowsDTO.setAmount(order.getAmount());
            rowsDTO.setOrders(order.getOrders());
            rowsDTO.setCustomers(order.getCustomers());
            rowsDTO.setDay(order.getDay());
            rowsDTO.setPcr(order.getPcr());
            rowsDTOS.add(rowsDTO);
        }

        statOrderVO.setRows(rowsDTOS);

        return statOrderVO;
    }

    @Override
    public StatGoodsVO goods() {
        //get StatGooderOrder
        List<StatGoodsMapperVO> statGoods = goodsMapper.getStatGoodsGroupByDate();

        //return StatGoodVO
        StatGoodsVO statGoodsVO = new StatGoodsVO();

        //set columns
        ArrayList<String> columns = new ArrayList<>();
        columns.add("day");
        columns.add("orders");
        columns.add("products");
        columns.add("amount");
        statGoodsVO.setColumns(columns);


        //set Rows
        ArrayList<StatGoodsVO.RowsDTO> rowsDTOS = new ArrayList<>();

        for (StatGoodsMapperVO goods : statGoods) {
            StatGoodsVO.RowsDTO rowsDTO = new StatGoodsVO.RowsDTO();
            rowsDTO.setAmount(goods.getAmount());
            rowsDTO.setDay(goods.getDay());
            rowsDTO.setOrders(goods.getOrders());
            rowsDTO.setProducts(goods.getProducts());
            rowsDTOS.add(rowsDTO);
        }

        statGoodsVO.setRows(rowsDTOS);


        return statGoodsVO;
    }
}